package com.ocom.aiotweb.controller;


import com.ocom.aiotweb.service.ConsumeDeviceService;
import com.ocom.aiotweb.vo.consumedevice.AddVo;
import com.ocom.aiotweb.vo.consumedevice.DeleteVo;
import com.ocom.aiotweb.vo.consumedevice.OnShowVo;
import com.ocom.aiotweb.vo.consumedevice.UpgradeVo;
import com.ocom.common.constant.AiotUrl;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@RestController
public class ConsumeDeviceController {

    @Autowired
    private ConsumeDeviceService consumeDeviceService;


    // 消费设备列表（ConsumeDevice/onShow）
    @PostMapping(AiotUrl.URL_Aiot_ConsumeDeviceOnShow)
    public Result ConsumeDeviceOnShow(@Validated @RequestBody OnShowVo onShowVo){
        return consumeDeviceService.ConsumeDeviceOnShow(onShowVo);
    }

    //消费设备新增（ConsumeDevice/add）
    @PostMapping(AiotUrl.URL_Aiot_ConsumeDeviceAdd)
    public Result ConsumeDeviceAdd(String saasNo,Long comId,Long agentId,Long commMode,Integer deviceType,Integer deviceVerType,@RequestPart("file") MultipartFile file){

        return consumeDeviceService.ConsumeDeviceAdd(saasNo,comId,agentId,commMode,deviceType,deviceVerType,file);
    }

    // 消费设备删除（ConsumeDevice/delete）
    @PostMapping(AiotUrl.URL_Aiot_ConsumeDeviceDelete)
    public Result ConsumeDeviceDelete(@Validated @RequestBody DeleteVo deleteVo){
        if (deleteVo.getDeviceNo() == null){
            log.error("删除设备出厂编号为空");
            return ResultUtil.error(ResultEnum.Err_203,"删除设备出厂编号为空");
        }
        return consumeDeviceService.ConsumeDeviceDelete(deleteVo);
    }


    /**
     * 导入机器模板（ConsumeDevice/importDeviceTemplate）
     * */
    @GetMapping(AiotUrl.URL_Aiot_ConsumeDeviceImportDeviceTemplate)
    public void ImportDeviceTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<AddVo> util = new ExcelUtil<AddVo>(AddVo.class);
        util.importTemplateExcel(response, "设备数据");
    }

    // 消费升级版本列表（ConsumeDevice/upgradeList）
    @GetMapping(AiotUrl.URL_Aiot_ConsumeDeviceUpgradeList)
    public Result ConsumeDeviceUpgradeList(String appVer,Integer deviceType){
        if (appVer == null ){
            log.error("设备版本为空");
            return ResultUtil.error(ResultEnum.Err_203,"设备版本为空");
        }
        if ( deviceType == null){
            log.error("设备类型为空");
            return ResultUtil.error(ResultEnum.Err_203,"设备类型为空");
        }
        return consumeDeviceService.ConsumeDeviceUpgradeList(appVer,deviceType);
    }


    // 消费设备升级（ConsumeDevice/upgrade）
    @PostMapping(AiotUrl.URL_Aiot_ConsumeDeviceUpgrade)
    public Result ConsumeDeviceUpgrade(@Validated  @RequestBody UpgradeVo upgradeVo){
        if (upgradeVo.getDeviceNo() == null ){
            log.error("设备编号为空");
            return ResultUtil.error(ResultEnum.Err_203,"设备编号为空");
        }
        if ( upgradeVo.getUpgradeVer() == null){
            log.error("版本号为空");
            return ResultUtil.error(ResultEnum.Err_203,"版本号为空");
        }
        return consumeDeviceService.ConsumeDeviceUpgrade(upgradeVo);
    }


}
